//========encoding:utf-8=========
/*
 * @describe:  三目运算符
 * @author:mhao
 * @copyright:chinasoft.cn
 * @date:2020年3月24日   下午11:32:05
 */
package edu.chinasoft.handler;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import edu.chinasoft.domain.User;

@Controller("userHandler")
@RequestMapping("/userhandler")
public class UserHandler {
	@RequestMapping(value="/login.do",method=RequestMethod.POST)
	public String method01(@Validated User user,BindingResult br,HttpSession session) {
		System.out.println(user.getName()+":"+user.getPassword());
		session.removeAttribute("nameerror");
		session.removeAttribute("passworderror");
		if(br.getErrorCount()>0) {
			//校验结果里有没有出错内容
			FieldError nameFild = br.getFieldError("name");
			//获取关于属性name的出错结果
			if(nameFild!=null) {
				//获取错误的提示信息
				String nameFildInfo = nameFild.getDefaultMessage();
				//把提示信息保存到session里，然后在登录页面显示
				session.setAttribute("nameerror", nameFildInfo);
			}
			FieldError passwordError = br.getFieldError("password");
			if(passwordError!=null) {
				String passwordFildInfo = passwordError.getDefaultMessage();
				session.setAttribute("passworderror", passwordFildInfo);
			}
			return "redirect:/login.jsp";
		}
		return "redirect:/login_success.jsp";
		

	}
}
